﻿using System;
using RabbitMQ.Client;

namespace ConsoleApp.Infrastructure.Rmq
{
    public class RmqFailService
    {
        public string QueueName { get; set; }

		public RmqOption RmqOption { get; set; }

		protected string FailQueueName => RmqBasicInfoConfig.BaseFailQueuePrefix + QueueName;

        public RmqFailService(string queueName, RmqOption rmqOption)
        {
            QueueName = queueName;
			RmqOption = rmqOption;
            try
            {
                using IModel model = RmqConnectPoolFactory.GetClientPool(RmqOption).GetConnect().GetChannel();
                model.QueueDeclare(FailQueueName, durable: true, exclusive: false, autoDelete: false, null);
                model.QueueBind(FailQueueName, RmqBasicInfoConfig.BaseExchange, FailQueueName, null);
            }
            catch (Exception ex)
            {
                RmqLogHelper.WriteError("FailQueueInit 初始化错误队列异常：" + ex.ToString());
            }
        }

        public void FailSend<T>(T t)
        {
            try
            {
                RmqMessageProducer.MessageSend(RmqOption, RmqBasicInfoConfig.BaseExchange, FailQueueName, t);
            }
            catch (Exception ex)
            {
                RmqLogHelper.WriteError("FailSend处理错误消息异常：" + ex.ToString());
            }
        }
    }
}
